import os
import re
import json 
import argparse 


def extrac_sub_parent(input, output):
    files = os.listdir(input)
    result_dict = {}
    suit = []
    no_suit = []
    for f in files:
        f_path = os.path.join(input, f)
        with open(f_path) as file:
            content = json.load(file)
            
        for k,v in content.items():
            is_suitable_match = re.search(r'"is_suitable":\s*(True|False)', v)
            is_suitable_value = is_suitable_match.group(1)
            
            if is_suitable_value == "True":
                concept_match = re.search(r'"concept":\s*"([^"]+)"', v)
                concept_value = concept_match.group(1)
                suit.append(suit)
                result_dict[k] = concept_value
                
            else:
                print(v,"\n\n\n")
                no_suit.append(v) 
    # print(len(suit))
    # print(len(no_suit))
    # for k,v in result_dict.items():
    #     print(k, "\t",  v)  
    
    with open(output, mode="w") as file:
        json.dump(result_dict, file, indent=4)
    
    print(output)


def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument("-input", "--input")
    parser.add_argument("-output", "--output")
    params = parser.parse_args()
    
    return params 


if __name__ == "__main__":
    params = parse_args()
    input = params.input 
    output = params.output
    
    extrac_sub_parent(input, output)
    